//
//  WDFinancialDetailController.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2016/12/9.
//  Copyright © 2016年 王笛. All rights reserved.
//

#import "WDFinancialDetailController.h"
#import "WDParentDetail.h"
#import "WDProductDetail.h"
#import "WDParentDetailCell.h"

@interface WDFinancialDetailController () <UITableViewDelegate, UITableViewDataSource>
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** 产品详情数据 */
@property (nonatomic, strong) NSArray *parentDetails;
@end

@implementation WDFinancialDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置导航条
    [self loadNavItemWithAction:@selector(clickBackBtn) btnImage:@"icon_back" btnSelImage:@"icon_back" title:self.productDetail.parent_name];
    
    [self setUpTableView];
}

- (void)clickBackBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 创建控件
- (void)setUpTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    
    tableView.backgroundColor = [UIColor whiteColor];
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
}

#pragma mark - lazy load
//产品详情数据
- (NSArray *)parentDetails
{
    if (_parentDetails == nil) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"WDParentDetail.plist" ofType:nil];
        
        NSArray *dictArr = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *parentDetailArr = [NSMutableArray array];
        NSDictionary *dict;
        
        for (dict in dictArr) {
            WDParentDetail *parentDetail = [WDParentDetail productDetailWithDict:dict];
            [parentDetailArr addObject:parentDetail];
        }
        _parentDetails = parentDetailArr;
    }
    return _parentDetails;
}

#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.parentDetails.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WDParentDetailCell *cell = [WDParentDetailCell parentDetailCellWithTableView:tableView];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.parentDetail = self.parentDetails[indexPath.row];
    
    switch (indexPath.row) {
        case 0:
        {
            cell.value = self.productDetail.applyto_business_value;
        }
            break;
            
        case 1:
        {
            cell.value = self.productDetail.type_value;
        }
            break;
            
        case 2:
        {
            cell.value = self.productDetail.system_url;
            if (![self.productDetail.system_url isEqualToString:@""]) {
                cell.textColor = ColorForRGB(0x4a90e2);
                cell.selectionStyle = UITableViewCellSelectionStyleGray;
            }
        }
            break;
            
        case 3:
        {
            cell.value = self.productDetail.mobile;
            if (![self.productDetail.mobile isEqualToString:@""]) {
                cell.textColor = ColorForRGB(0x4a90e2);
                cell.selectionStyle = UITableViewCellSelectionStyleGray;
            }
        }
            break;
            
        case 4:
        {
            cell.value = self.productDetail.link_name;
        }
            break;
            
        case 5:
        {
            cell.value = self.productDetail.link_phone;
            if (![self.productDetail.link_phone isEqualToString:@""]) {
                cell.textColor = ColorForRGB(0x4a90e2);
                cell.selectionStyle = UITableViewCellSelectionStyleGray;
            }
        }
            break;
            
        case 6:
        {
            cell.value = self.productDetail.link_mobile;
            if (![self.productDetail.link_mobile isEqualToString:@""]) {
                cell.textColor = ColorForRGB(0x4a90e2);
                cell.selectionStyle = UITableViewCellSelectionStyleGray;
            }
        }
            break;
            
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 2:
        {
            //跳转网页
            if (![self.productDetail.system_url isEqualToString:@""]) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.productDetail.system_url]];
            }
            
        }
            break;
            
        case 3:
        {
            //拨打电话
            NSURL *openURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", self.productDetail.mobile]];
            
            [[UIApplication sharedApplication] openURL:openURL];
        }
            break;
            
        case 5:
        {
            //拨打电话
            NSURL *openURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", self.productDetail.link_phone]];
            
            [[UIApplication sharedApplication] openURL:openURL];
        }
            break;
            
        case 6:
        {
            //拨打电话
            NSURL *openURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", self.productDetail.link_mobile]];
            
            [[UIApplication sharedApplication] openURL:openURL];
        }
            break;
            
        default:
            break;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc] init];
    
    UIImageView *iconView = [[UIImageView alloc] init];
    [iconView sd_setImageWithURL:[NSURL URLWithString:self.productDetail.parent_logo]];
    [headView addSubview:iconView];
    
    [iconView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(20);
        make.centerX.equalTo(headView.centerX);
        make.width.equalTo(112);
        make.height.equalTo(80);
    }];
    
    return headView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 151;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
